package com.pai4j.ugc.service;
import com.pai4j.common.bean.PAIResponseBean;
import com.pai4j.common.bean.PAIResponseBeanUtil;
import com.pai4j.common.enums.AccountRelationTypeEnum;
import com.pai4j.domain.vo.response.UserBaseResponseInfoVO;
import com.pai4j.remote.user.UserServiceClient;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class AccountService {

    @Autowired
    private UserServiceClient userServiceClient;

    /**
     * 查询当前账号信息
     *
     * @param account
     * @return
     */

    public UserBaseResponseInfoVO accountInfo(String account) {

        PAIResponseBean<UserBaseResponseInfoVO> responseBean = userServiceClient.getUserBaseInfo(account);
        if (PAIResponseBeanUtil.isOk(responseBean)) {
            return responseBean.getData();
        }
        return null;
    }

    /**
     *  批量查询账号详情数据
     *
     * @param accounts
     * @return
     */
    public List<UserBaseResponseInfoVO> batchGet(List<String> accounts) {
        if (CollectionUtils.isEmpty(accounts)) {
            return Collections.emptyList();
        }
        PAIResponseBean<List<UserBaseResponseInfoVO>> responseBean =
                userServiceClient.batchGetList(accounts);
        if (PAIResponseBeanUtil.isOk(responseBean)) {
            return responseBean.getData();
        }
        throw new RuntimeException(responseBean.getMessage());
    }

    public Map<String, UserBaseResponseInfoVO> batchGet(Set<String> accounts) {
        if (CollectionUtils.isEmpty(accounts)) {
            return Collections.emptyMap();
        }
        PAIResponseBean<Map<String, UserBaseResponseInfoVO>> responseBean =
                userServiceClient.batchGetMap(accounts);
        if (PAIResponseBeanUtil.isOk(responseBean)) {
            return responseBean.getData();
        }
        throw new RuntimeException(responseBean.getMessage());
    }

    /**
     * 获取群组下的群成员用户id集合
     *
     * @param groupAccount
     * @return
     */
    public Set<String> getGroupUserIds(String groupAccount) {
        PAIResponseBean<Set<String>> responseBean = userServiceClient.getAllSubscriberByAccount(
                AccountRelationTypeEnum.USER_GROUP.getType(), groupAccount);
        if (PAIResponseBeanUtil.isOk(responseBean)) {
            return responseBean.getData();
        }
        return null;
    }

    /**
     * 获取群组下的群成员用户id集合
     *
     * @return
     */
    public Set<String> getAllSubscriberByAccount(String account, AccountRelationTypeEnum accountRelationType) {
        PAIResponseBean<Set<String>> responseBean =
                userServiceClient.getAllSubscriberByAccount(accountRelationType.getType(), account);
        if (PAIResponseBeanUtil.isOk(responseBean)) {
            return responseBean.getData();
        }
        return null;
    }


    /**
     * 判断userAccount是否订阅account
     *
     * @param userAccount
     * @param account
     * @param accountRelationType
     * @return
     */
    public boolean isSubscribe(String userAccount, String account, AccountRelationTypeEnum accountRelationType) {
        PAIResponseBean<Boolean> responseBean =
                userServiceClient.isSubscribe(accountRelationType.getType(), userAccount, account);
        if (PAIResponseBeanUtil.isOk(responseBean)) {
            return ObjectUtils.equals(responseBean.getData(), true);
        }
        return false;
    }
}
